<?php
namespace Web\Controller;
class SearchController extends CommonController {
    /**
     * 搜索列表
     *
     * @param    void
     * @return    void
    **/
    public function index(){
        $keyword = I('get.keyword');

        $ArticleModel = D('Article');

        $p = I('get.p',1,'intval');

        $offset = ($p -1 ) * C('PER_PAGE_NUM');

        $where = array(
            '_complex'  =>  array(
                '_logic'    =>  'or',
                'title'     =>  array('like',"%{$keyword}%"),
                'author'    =>  array('like',"%{$keyword}%"),
                'source_name'=>  array('like',"%{$keyword}%"),
                'tags'      =>  array('like',"%{$keyword}%"),
            ),
            'status'    =>  1,
            'publishtime'   =>  array('lt',NOW_TIME),
            'sid'       =>  C('sid')
        );

        $count  = $ArticleModel->where($where)->count();// 查询满足要求的总记录数

        if($count) {
            $list = $ArticleModel->limit($offset.','.C('PER_PAGE_NUM'))->where($where)->order('publishtime desc')->select();
        }else {
            $list = array();
        }

        $this->assign(array(
            'article_list'  =>  $list,
            'article_count' =>  $count,
            'keyword'       =>  $keyword,
        ));
        $this->display();
    }
}
